﻿using UnityEngine;
using UnityEngine.Android;

namespace BToolkit
{
    public class AndroidUtils
    {
        static string[] permissions = new string[] {
            "android.permission.INTERNET",
            "android.permission.READ_PHONE_STATE",
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.ACCESS_WIFI_STATE",
            "android.permission.ACCESS_NETWORK_STATE",
            "android.permission.GET_TASKS",
            "android.permission.REQUEST_INSTALL_PACKAGES",
            "android.permission.WAKE_LOCK",
            "android.permission.SYSTEM_ALERT_WINDOW",
            "android.permission.CHANGE_WIFI_STATE",
            "android.permission.CHANGE_NETWORK_STATE",
            "android.permission.ACCESS_COARSE_LOCATION",
            "android.permission.ACCESS_FINE_LOCATION",
            "android.permission.SYSTEM_OVERLAY_WINDOW",
            "android.permission.ACCESS_COARSE_UPDATES",
            "android.permission.WRITE_SETTINGS",
            "android.permission.BATTERY_STATS",
            "android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
        };

        static AndroidJavaObject jo;

        /// <summary>
        /// 动态请求安卓权限
        /// </summary>
        public static void RequestPermissions()
        {
#if UNITY_ANDROID
            for (int i = 0; i < permissions.Length; i++)
            {
                string s = permissions[i];
                if (!Permission.HasUserAuthorizedPermission(s))
                {
                    Permission.RequestUserPermission(s);
                }
            }
#endif
        }


        /// <summary>
        /// 调用Java(Android)层的非静态函数
        /// </summary>
        public static void CallAndroidFunction(string functionName, params object[] args)
        {
            if (jo == null)
            {
                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
            }
            jo.Call(functionName, args);
        }
        public static T CallAndroidFunction<T>(string functionName, params object[] args)
        {
            if (jo == null)
            {
                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
            }
            return jo.Call<T>(functionName, args);
        }
        /// <summary>
        /// 调用Java(Android)层的静态函数，无返回值
        /// </summary>
        public static void CallAndroidStaticFunction(string classPath, string methodName, params object[] args)
        {
            AndroidJavaClass jc = new AndroidJavaClass(classPath);
            jc.CallStatic(methodName, args);
        }
        /// <summary>
        /// 调用Java(Android)层的静态函数，有返回值
        /// </summary>
        public static T CallAndroidStaticFunction<T>(string classPath, string methodName, params object[] args)
        {
            AndroidJavaClass jc = new AndroidJavaClass(classPath);
            return jc.CallStatic<T>(methodName, args);
        }

    }
}